package com.zyc.app.woa.fragment;

import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import com.zyc.app.woa.utils.FragmentUtil;
import com.zyc.app.woa.utils.Trace;

/**
 * Author: yingchang.zhang
 * Date  : 2014/8/13
 */
public class BaseFragment extends Fragment {
    private final static int STATE_UNLOAD = 0;
    private final static int STATE_LOADING = 1;
    private final static int STATE_LOADED = 2;
    private int mLoadingState = STATE_UNLOAD;
    protected FragmentUtil mFragmentUtil = new FragmentUtil();

    @Override
    public void onStart() {
        super.onStart();
        if (STATE_UNLOAD == mLoadingState) {
            mLoadingState = STATE_LOADING;
            startLoadData();
            mLoadingState = STATE_LOADED;
        }
    }

    public void startLoadData() {
        //start load data.
    }

    public void hideActionBar() {
        mFragmentUtil.setActionBarVisible(getActivity(), false);
    }

    public void showActionBar() {
        mFragmentUtil.setActionBarVisible(getActivity(), true);
    }

    public void setTitle(String title) {
        mFragmentUtil.setTitle(getActivity(), title);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Trace.log("fragment back key: " + keyCode + " event: " + event.toString());
        return false;
    }

    public void finishCurrFragment() {
        mFragmentUtil.finishCurrFragment(getActivity());
    }

    public void launchFragment(BaseFragment fragment) {
        mFragmentUtil.launchFragmentInActivity(getActivity(), fragment);
    }

    public void launchFragment(int viewId, BaseFragment fragment) {
        mFragmentUtil.launchFragmentInActivity(getActivity(), viewId, fragment);
    }

    public void launchSubFragment(int viewId, BaseFragment fragment) {
        FragmentUtil.launchFragment(getChildFragmentManager(), viewId, true, fragment);
    }

    public String getFragmentTag() {
        return this.getClass().getCanonicalName();
    }
}
